﻿///<reference path="_moduleReferences.ts" />

module ScrambledEggs.Modules {
    export class Meta implements IModule {
        private _suggestionbox: JQuery;
        private _onUpdate: () => void;

        constructor() {
            this._suggestionbox = $("#suggestionbox").remove();
        }

        initialize(gameState: GameStateProvider, onUpdate: () => void ) {
            var $doc = $(document);
            var me = this;
            me._onUpdate = onUpdate;
            
            var saveAlertTimeout: number = null;
            var save = function () {
                clearTimeout(saveAlertTimeout);
                saveAlertTimeout = setTimeout(saveAlert, 600000);
                gameState.save(function () {
                    gameState.State.Message = "Game saved on " + Utility.Format.Date(new Date()) + " - <a href=\"" + window.location.href.split('?')[0] + "?key=" + gameState.Key + "\">This is the link to your current game.</a>";
                    onUpdate();
                });
            }
            var saveAlert = function () {
                clearTimeout(saveAlertTimeout);
                saveAlertTimeout = setTimeout(saveAlert, 600000);
                Utility.General.YesNoBox("You haven't saved your progress in a while. Would you like to save?", save, $.noop);
            }
            saveAlertTimeout = setTimeout(saveAlert, 600000);

            $("button.save").click(function () {
                save();
            });
            $("button.hints").click(function () {
                gameState.State.HintsOn = !gameState.State.HintsOn;
                if (gameState.State.HintsOn) {
                    $(this).html("Hints: ON");
                    var rnd = Utility.Number.Random(0, 6);
                    switch (rnd) {
                        case 0:
                            $("#hint").html("Phew, <strong>thanks</strong>!! <i>:)</i>");
                            break;
                        case 1:
                            $("#hint").html("Hey, my name is <strong>Frank</strong>, by the way!");
                            break;
                        case 2:
                            $("#hint").html("Hello again, how are <strong>you</strong> doing?!");
                            break;
                        case 3:
                            $("#hint").html("You <strong>SAVED ME</strong>. How can I <strong>EVER</strong> repay you?!");
                            break;
                        case 4:
                            $("#hint").html("You can't <strong>believe</strong> how long I've been waiting for this!");
                            break;
                        case 5:
                            $("#hint").html("Next time you lock me up I swear I'm downloading a <strong>virus</strong> or something!");
                            break;
                    }
                    $("#hint").stop().show().css("opacity", 1);
                }
                 else {
                     $(this).html("Hints: OFF");
                     $("#hint").html("<strong>NO! HOW COULD YOU?!!</strong> <i>D:</i>").stop().fadeOut(3000);
                 }
            });
            $doc.on("mouseenter", "[data-hint]", function () {
                if (!gameState.State.HintsOn)
                    return;
                var $el = $(this);
                $("#hint").html($el.data("hint")).fadeIn("fast");                
            }).on("mouseleave", "[data-hint]", function () {
                if (!gameState.State.HintsOn)
                    return;
                $("#hint").fadeOut("fast");
            });
            $doc.on("click", "button.suggest", function () {
                Utility.General.InfoBox(me._suggestionbox.html(), "Return to game", function () { me._onUpdate(); });
            });
            
            $doc.on("click", "button.postsuggestion", function () {
                var button = this;
                var text = $(".suggestionContent").val();
                $.ajax({
                    url: "/Api/Suggestions",
                    method: "PUT",
                    data: { SaveKey: gameState.Key, Text: text }
                }).done(function () {
                    $("<p><strong><br />Thank you!<br />&nbsp;</strong></p>").insertAfter($(button).parent());
                    $(button).parent().remove();
                });
            });
        }

        update(gameState: GameStateProvider, triggerSource: IModule): void {
            $("#meta").toggle(gameState.State.Phase != GamePhase.Fetus);
            $("button.hints").html(gameState.State.HintsOn ? "Hints: ON" : "Hints: OFF");
        }        
    }
}